package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"group-card/common/constant"
	"group-card/common/request"
	"group-card/model/entity"
	"group-card/rpc/card/cardclient"
	"group-card/rpc/card/internal/svc"
)

type PublishListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPublishListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PublishListLogic {
	return &PublishListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PublishListLogic) PublishList(in *cardclient.PublishListRequest) (*cardclient.PublishListResponse, error) {
	var ctx context.Context
	req := &entity.CardPublishReq{
		BaseReq: request.BaseReq{
			PageNum:  in.PageNum,
			PageSize: in.PageSize,
		},
	}
	data, count, err := l.svcCtx.PublishModel.List(ctx, req)
	if err != nil {
		return nil, err
	}

	var list []*cardclient.PublishListData
	for _, value := range data {
		list = append(list, &cardclient.PublishListData{
			Id:                 value.Id,
			PublishTime:        value.PublishTime.Format(constant.YMDHIS),
			AdminId:            value.AdminId,
			CustomerId:         value.CustomerId,
			SkinId:             value.SkinId,
			Count:              value.Count,
			BeginNumber:        value.BeginNumber,
			EndNumber:          value.EndNumber,
			Status:             value.Status,
			Type:               value.Type,
			ServiceNumber:      value.ServiceNumber,
			ServiceOrderNumber: value.ServiceOrderNumber,
			ServiceMoney:       value.ServiceMoney,
			CreateAt:           value.CreateAt.Format(constant.YMDHIS),
			UpdateAt:           value.UpdateAt.Format(constant.YMDHIS),
			DeleteAt:           value.DeleteAt.Format(constant.YMDHIS),
		})
	}

	return &cardclient.PublishListResponse{
		Total: count,
		List:  list,
	}, nil

}
